﻿#include "MyThread.h"
#include "Global.h"
rf_card* card;
MyThread::MyThread(QObject *parent) : QThread(parent)
{

    card = initCard(card);
    count = 0;
    connect(&Count, SIGNAL(timeout()), this, SLOT(QTimer_Count()));
    printf("刷卡线程: %p\n", card);

#if (PRODUCER == 1)
    char devNo[8];
    char psam[6];
    db_register* data = new db_register;
    data->db_tran_type = TABLE_QL;
    qDebug("返回值%d",protocol_database_register(data));
    QString devNoStr = readFile(QString(CHARGE_CONF), QString("devNO"));
    QString psamStr = readFile(QString(CHARGE_CONF), QString("CARD_PSAM"));
    qDebug()<<devNoStr;
    qDebug()<<psamStr;
    AsciiToHex(devNoStr.toLatin1().data(), (unsigned char*)devNo, 16);
    AsciiToHex(psamStr.toLatin1().data(), (unsigned char*)psam, 12);
    for(int i=0; i<8; i++)//devNoStr
    {
        printf("%x\n",devNo[i]);
    }
    printf("\nssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss\n");
    for(int i=0; i<6; i++)//psamStr
    {
        printf("%x\n",psam[i]);
    }
    printf("\nddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddds\n");

    protocol_yct_card_set_info(psam, devNo);
#else (PRODUCER == 6)

#endif
}

short* MyThread::getCardNum(void)
{
    return card->cardNUM;
}

QString MyThread::getCardType(void)
{
    return QString("%1").arg(card->cardtype);
}

void MyThread::run()
{
#if 1
    float money = 0;
    char locknum = 0;

    if(!Count.isActive() && stackLayout->currentIndex() == chargeWatchingDCPage)
        Count.start(1000);

    if( !protocol_read_card(&money, &locknum) )
    {
        QString data = "";
        for(int i=0; i<8; i++)
        {
            data += QString("%1").arg(card->cardNUM[i], 2, 16, QLatin1Char('0'));
            // 这个方法用于填充字符串中的%1,%2...为给定格式的整形数字，其中第一个参数是要填充的数字，
            // 第二个参数为最小宽度，第二个参数为进制，第四个参数为当原始数字长度不足最小宽度时用于填充的字符。
        }
        if(stackLayout->currentIndex() == chargeWatchingDCPage)
        {
            if(count > 2)
                emit sendData(data, 0, money);
        }
        else if(stackLayout->currentIndex() != chargeWatchingDCPage)
        {
            emit sendData(data, 0, money);
        }
    }
    else
    {
        emit sendData(QString(""), 1, 0);//刷卡失败的信号
    }
    if(Count.isActive())
    {
        count = 0;
        Count.stop();
    }
#endif
}

void MyThread::QTimer_Count()
{
    count = 3;
}

rf_card* MyThread::getCard()
{
    return card;
}

MyThread::~MyThread()
{

}

Gun_Current_State GUN_STATE[GUN_AMOUNT];
RefreshGun::RefreshGun(QObject *parent) : QThread(parent)
{
    GUN_NO = -1;
    swipCardflag = 0;
    VINcharge = 0;
    appcharge = 0;
    swipcard_YCT = 0;
    // update rfid
    connect(&updateQrCode,SIGNAL(timeout()),this,SLOT(UpdateQRCode()));
    // flags : gun in charging
    memset((void*)&putInChargerFlag[0], 0, sizeof(putInChargerFlag));
    // update error info
    ErrorTimer = new QTimer(this);
    connect(ErrorTimer, SIGNAL(timeout()), this, SLOT(update_ErrorInfomation()));
    // update charging info
    connect(&Charging_Monitoring, SIGNAL(timeout()), this, SLOT(Moitoring_feedback()));
}

void RefreshGun::run()
{
    GUN_STATE[GUN_NO].Gun_Num = GUN_NO;
    qDebug()<<QString("刷新线程启动")<<GUN_NO<<QString("枪编号:")<<GUN_STATE[GUN_NO].Gun_Num;

#if (PRODUCER != 3 && PRODUCER != 5 && PRODUCER != 6 )
    GUN_STATE[GUN_NO].m_QrCodeNum = "";
#endif

#if ( PRODUCER == 0 )
    onGunQRCodeInfo(QString("0"), GUN_NO+1, GUN_NO, GUN_NO);
#elif ( PRODUCER == 1 )
    onGunQRCodeInfo(QString("-0"), GUN_NO+1, GUN_NO, GUN_NO);
#elif ( PRODUCER == 2 )
    onGunQRCodeInfo(QString("-"), GUN_NO, GUN_NO, GUN_NO);
#elif ( PRODUCER == 3 || PRODUCER == 5 || PRODUCER == 6 )  // update QR by server
    updateQrCode.start(500);
#elif ( PRODUCER == 4 )
    onGunQRCodeInfo(QString("000"), GUN_NO, GUN_NO, GUN_NO);
#endif
    //qDebug()<<QString("二维码:")<<GUN_STATE[GUN_NO].m_QrCodeNum;
    while(1)
    {
        msleep(500);
    }
}

RefreshGun::~RefreshGun()
{

}
